<?php

/**
 * This is the model class for table "announcement".
 *
 * The followings are the available columns in table 'announcement':
 * @property integer $id
 * @property string $title
 * @property string $content
 * @property string $description
 * @property integer $status
 * @property integer $event_fg
 * @property integer $position
 * @property integer $view_no
 * @property integer $join_no
 * @property string $created
 * @property string $updated
 */
class Announcement extends AnnouncementBase
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Announcement the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, content, created, user_id, event_end_date', 'required'),
			array('status, event_fg, position, view_no, join_no', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>100),
			array('updated', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, content, description, status, event_fg, position, view_no, join_no, created, updated, event_end_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Tiêu đề',
			'content' => 'Nội dung',
			'description' => 'Mô tả ngắn',
			'status' => 'Tình trạng',
			'event_fg' => 'Sự kiện',
			'position' => 'Vị trí',
			'view_no' => 'Đã xem',
			'join_no' => 'Tham dự',
			'created' => 'Ngày tạo',
			'updated' => 'Ngày sửa',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function getList($userId, $category)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria	= new CDbCriteria;
		$criteria->with = array('member_asign');
		$criteria->compare('member_id', $userId);
		$criteria->compare('event_fg', $category);
		$criteria->addCondition('t.status = :status');
		$criteria->params[':status']	= 1;
		$criteria->order	= 'created DESC';
		return new CActiveDataProvider($this, array(
			'criteria'	=> $criteria,
			'pagination' => array(
				'pageSize'	=> LIMIT_POST_RECORD,
			),
		));
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function searchPost($key)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria	= new CDbCriteria;
		$criteria->addSearchCondition('title', $key, true, 'OR');
		$criteria->addSearchCondition('content', $key, true, 'OR');
		$criteria->addSearchCondition('description', $key, true, 'OR');
		$criteria->order	= 'created DESC';
		return new CActiveDataProvider($this, array(
			'criteria'	=> $criteria,
			'pagination' => array(
				'pageSize'	=> 2,//LIMIT_POST_RECORD,
			),
		));
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function getContent($id)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;
		return $this->findByPk($id);
	}
}